<?php
class ControllerModuleProducts extends Controller {
	protected function index($setting) {
		$this->language->load('module/products'); 

      	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['button_cart'] = $this->language->get('button_cart');
		$this->data['view_all'] = $this->language->get('view_all');
		
		$this->load->model('catalog/product'); 
		
		$this->load->model('tool/image');
		
		$listcategories = $this->model_catalog_category->getCategories();
		$totalcategories = count($listcategories);
		for($i=0;$i<$totalcategories;$i++){
			$this->data['products'][]=$this->showproduct($listcategories[$i]["category_id"]);
			$this->data['category'][]=$listcategories[$i];
		}
		$this->data['totalcategories'] = $totalcategories;
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/products.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/products.tpl';
		} else {
			$this->template = 'default/template/module/products.tpl';
		}
		$this->data['vat'] = $this->language->get('text_vat');

		$this->render();
	}
	
	function showproduct($category_id){
		$p = array();
		$sort = 'rand()';
		$order = 'ASC';
		if($category_id==20){

			$data = array(
					'filter_category_id' => $category_id,
					'sort'               => $sort,
					
					'start'              => 0,
					'limit'              => 10
			);
		}else{
			$data = array(
					'filter_category_id' => $category_id,
					'sort'               => $sort,
					
					'start'              => 0,
					'limit'              => 5
			);
		}
				
		$product_total = $this->model_catalog_product->getTotalProducts($data); 
		
		$results = $this->model_catalog_product->getProducts($data);
		
		foreach ($results as $result) {
			if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
			} else {
				$image = false;
			}
			
			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
				$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$price = false;
			}
			
			if ((float)$result['special']) {
				$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$special = false;
			}	
			
			if ($this->config->get('config_tax')) {
				$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
			} else {
				$tax = false;
			}				
			
			if ($this->config->get('config_review_status')) {
				$rating = (int)$result['rating'];
			} else {
				$rating = false;
			}
			
			$sticker = $this->model_catalog_product->getProductsticker($result['ptype']);
				
			$p[] = array(
				'product_id'  => $result['product_id'],
				'thumb'       => $image,
				'name'        => $result['name'],
				'description' => substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
				'price'       => $price,
				'special'     => $special,
				'tax'         => $tax,
				'rating'      => $result['rating'],
				'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
				'sticker'	=> $sticker,
				'vat_shown'	=> $result['vat_shown'],
				'href'        => $this->url->link('product/product', 'path=' . $category_id . '&product_id=' . $result['product_id'])
			);
		}
		return $p;
	}
}
?>